package com.apestech.framework.sql.modul;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

public class VarList {
	
	private ArrayList<VarPos> list=new ArrayList<VarPos>();
	private String text="";

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void addInfo(int start, int end, String var, String symbol, String tab, Object value,
						String fieldType, String fieldValue, String notNull, String treeTable, String treeId, String treePId){
		int lastEnd=0;
		VarPos v=new VarPos();
		if(list.size()>0){
			lastEnd=list.get(list.size()-1).getEnd();
		}
		//要考虑初始化XML里变量信息和增加WHERE条件时两种情况
		if(value!=null){
			if(value instanceof JSONArray){
				value=null;
			} else if(value instanceof Map){
				JSONObject m=new JSONObject((Map)value);
				String operator=m.getString("operator");
				if("EQ".equals(operator)){
					value=m.get("value");
				} else {
					value=null;
				}
			}
			if(value==null){
				System.out.println("初始化SQL里的变量"+var+"存在传入参数为null情况，如该SQL为SELECT类型请留意该问题!");
			}
		}
		v.setInfo(start, end, lastEnd, var, symbol, tab, value, fieldType, fieldValue, notNull);
		if(treeTable!=null) v.setTree(treeTable, treeId, treePId);
		list.add(v);
	}
	
	public VarPos get(int index){
		if(index>list.size()-1) return null;
		return list.get(index);
	}
	
	public int size(){
		return this.list.size();
	}
	
	public String toString(){
		return "{text:"+text+","+this.list.toString()+"}";
	}

}
